package com.txzt.gaea.business.project.controller;


import com.txzt.base.core.RtnInfo;
import com.txzt.gaea.business.project.domain.model.ProjectNode;
import com.txzt.gaea.business.project.service.IProjectNodeService;
import com.txzt.gaea.business.util.TranslateUtil;
import com.txzt.gaea.common.annotation.EntityCommonValue;
import com.txzt.gaea.common.base.controller.BaseController;
import com.txzt.gaea.common.core.base.context.AdminUserContext;
import com.txzt.gaea.common.enums.IsDeleteEnum;
import com.txzt.gaea.vo.AdminUserVO;
import org.beetl.sql.core.engine.PageQuery;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/project/node")
public class ProjectNodeController extends BaseController {


    private static final Logger logger = LoggerFactory.getLogger(ProjectNodeController.class);


    @Autowired
    private IProjectNodeService projectNodeService;

    /**
     * 分页查询
     * @param pageNumber
     * @param pageSize
     * @param projectNode
     * @return
     */
    @GetMapping(value = "/list")
    public RtnInfo<?> pageList(@RequestParam(defaultValue = "1") Integer pageNumber,
                               @RequestParam(defaultValue = "10") Integer pageSize, ProjectNode projectNode) {
        logger.info(projectNode.toString());
        PageQuery<ProjectNode> pageList = projectNodeService.pageQuery(new PageQuery<>(pageNumber,pageSize,projectNode));
        return RtnInfo.success(this.translate(pageList));
    }

    /**
     * 分页查询
     * @param pageNumber
     * @param pageSize
     * @param projectNode
     * @return
     */
    @GetMapping(value = "/listCheck")
    public RtnInfo<?> listCheck(@RequestParam(defaultValue = "1") Integer pageNumber,
                               @RequestParam(defaultValue = "10") Integer pageSize, ProjectNode projectNode) {
        logger.info(projectNode.toString());
        AdminUserVO adminUserVO = AdminUserContext.getUser();
        List<Long> roleIds = Arrays.stream(adminUserVO.getRoleid().split(",")).mapToLong(Long::valueOf).boxed().collect(Collectors.toList());
        projectNode.setRoleIds(roleIds);
        PageQuery<ProjectNode> pageList = projectNodeService.pageQuery(new PageQuery<>(pageNumber,pageSize,projectNode));
        return RtnInfo.success(this.translate(pageList));
    }

    private PageQuery<ProjectNode> translate(PageQuery<ProjectNode> pageList){
        pageList.getList().forEach(item -> {
            //翻译创建人
            item.setCreateName(TranslateUtil.getUserName(item.getCreateBy()));
            //翻译更新人
            item.setUpdateName(TranslateUtil.getUserName(item.getUpdateBy()));
        });
        return pageList;
    }


    /**
     * id查询
     * @param id
     * @return
     */
    @GetMapping(value = "/queryById")
    public RtnInfo<?> queryById(@RequestParam Long id) {
        ProjectNode projectNode = projectNodeService.selectProjectNodeById(id);
        if (null == projectNode) {
            return RtnInfo.SERVER_ERROR;
        }else {
            return RtnInfo.success(projectNode);
        }

    }



    /**
     * 新增
     * @param projectNode
     * @return
     */
    @PostMapping(value = "/add")
    @EntityCommonValue(operation = "CREATE")
    public RtnInfo<?> add(@RequestBody ProjectNode projectNode) {
        int count = projectNodeService.saveProjectNode(projectNode);
        if (count > 0) {
            return RtnInfo.SUCCESS;
        } else {
            return RtnInfo.SERVER_ERROR;
        }
    }


    /**
     * 编辑
     * @param projectNode
     * @return
     */
    @PutMapping(value = "/edit")
    @EntityCommonValue(operation = "UPDATE")
    public RtnInfo<?> edit(@RequestBody ProjectNode projectNode) {
        int count = projectNodeService.editProjectNode(projectNode);
        if (count > 0) {
            return RtnInfo.SUCCESS;
        } else {
            return RtnInfo.SERVER_ERROR;
        }
    }


    /**
     * 重新提交
     * @param projectNode
     * @return
     */
    @PutMapping(value = "/resubmit")
    @EntityCommonValue(operation = "UPDATE")
    public RtnInfo<?> resubmit(@RequestBody ProjectNode projectNode) {
        int count = projectNodeService.resubmitProjectNode(projectNode);
        if (count > 0) {
            return RtnInfo.SUCCESS;
        } else {
            return RtnInfo.SERVER_ERROR;
        }
    }


    /**
     * 逻辑删除
     * @param id
     * @return
     */
    @DeleteMapping(value = "/delete")
    @EntityCommonValue(operation = "UPDATE")
    public RtnInfo<?> delete(@RequestParam Long id) {
        ProjectNode projectNode = new ProjectNode();
        projectNode.setId(id);
        projectNode.setIsDelete(IsDeleteEnum.DELETE.getCode());
        int count = projectNodeService.updateById(projectNode);
        if (count > 0) {
            return RtnInfo.SUCCESS;
        } else {
            return RtnInfo.SERVER_ERROR;
        }
    }

}
